package com.lanyuan.a.controller;


import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanyuan.a.atest.BaseFormMap;
import com.lanyuan.a.atest.UserFormMap;
import com.lanyuan.a.bean.Order;
import com.lanyuan.a.bean.User;
import com.lanyuan.a.bean.Window;
import com.lanyuan.a.bean.WindowFood;
import com.lanyuan.a.service.ButtonService;
import com.lanyuan.a.service.OrderService;
import com.lanyuan.a.service.WindowFoodService;
import com.lanyuan.a.service.WindowSaleService;
import com.lanyuan.a.service.WindowService;
import com.lanyuan.a.service.union.UserManageService;
import com.lanyuan.controller.index.BaseController;
import com.lanyuan.exception.SystemException;
import com.lanyuan.plugin.PageView;
import com.lanyuan.util.Common;

@Controller
@RequestMapping("/usermanage/")
public class UserManageController extends BaseController {
	@Autowired
	UserManageService userManageService;
	@Autowired
	WindowSaleService windowSaleService;
	@Resource
	WindowService windowService;
	@Autowired
	ButtonService buttonService;
	@Resource
	WindowFoodService windowFoodService;
	@Resource
	OrderService orderService;
	
	@RequestMapping("list")
	public String listUI(Model model) throws Exception {
		model.addAttribute("windows", windowService.list());
		model.addAttribute("buttons", buttonService.getButtons("#user"));
		return Common.BACKGROUND_PATH + "/system/usermanage/list";
	}

	@ResponseBody
	@RequestMapping("findByPage")
	public PageView findByPage(String pageNow,String pageSize) throws Exception {
		System.out.println();
		BaseFormMap baseFormMap = getFormMap(BaseFormMap.class);
		baseFormMap=toFormMap(baseFormMap, pageNow, pageSize);
		
		String wid = (String)baseFormMap.get("wid");
		String orderType = (String)baseFormMap.get("orderType");
		String orderDirection = (String)baseFormMap.get("orderDirection");
		if(wid == null) wid="1";
		
		if(orderType == null) orderType = "f_salenum";
		if(orderDirection == null) orderDirection = "desc";
		List<WindowFood> list = windowFoodService.queryWidByPage(baseFormMap , wid , orderType ,orderDirection);
		pageView.setRecords(userManageService.list(list));//不调用默认分页,调用自已的mapper中findUserPage
        return pageView;
	}

	@RequestMapping("addUI")
	public String addUI(Model model) throws Exception {
		return Common.BACKGROUND_PATH + "/system/usermanage/add";
	}

	@ResponseBody
	@RequestMapping("addEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	public String addEntity(String txtGroupsSelect){
		try {
			System.out.println("addEntity");
			UserFormMap userFormMap = getFormMap(UserFormMap.class);
			String username = (String)userFormMap.get("userName");
			String accountname = (String)userFormMap.get("accountName");
			String password = (String)userFormMap.get("password");
			User user = new User();
			user.setUsername(username);
			user.setAccountname(accountname);
			user.setPassword(password);
//			userService.saveUser(user);
		} catch (Exception e) {
			 throw new SystemException("添加账号异常");
		}
		return "success";
	}

	
	@RequestMapping("editUI")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		if(Common.isNotEmpty(id)){
			model.addAttribute("user", new UserFormMap().findById(id));
		}
		return Common.BACKGROUND_PATH + "/system/usermanage/edit";
	}

	
	@ResponseBody
	@RequestMapping("selectWindow")
	public PageView selectWindow(String pageNow,String pageSize){
			BaseFormMap baseFormMap = getFormMap(BaseFormMap.class);
			baseFormMap=toFormMap(baseFormMap, pageNow, pageSize);
			
			String wid = (String)baseFormMap.get("wid");
			String orderType = (String)baseFormMap.get("orderType");
			String orderDirection = (String)baseFormMap.get("orderDirection");
			if(wid == null) wid="1";
			
			if(orderType == null) orderType = "f_salenum";
			if(orderDirection == null) orderDirection = "desc";
			List<WindowFood> list = windowFoodService.queryWidByPage(baseFormMap , wid , orderType ,orderDirection);
			pageView.setRecords(userManageService.list(list));//不调用默认分页,调用自已的mapper中findUserPage
	        return pageView;
	}
	
	@ResponseBody
	@RequestMapping("selectFtype")
	public PageView selectFtype(String pageNow,String pageSize){
			BaseFormMap baseFormMap = getFormMap(BaseFormMap.class);
			baseFormMap=toFormMap(baseFormMap, pageNow, pageSize);
			
			String ftype = (String)baseFormMap.get("ftype");
			String orderType = (String)baseFormMap.get("orderType");
			String orderDirection = (String)baseFormMap.get("orderDirection");
			if(ftype == null) ftype="1";
			
			if(orderType == null) orderType = "f_name";
			if(orderDirection == null) orderDirection = "desc";
			List<WindowFood> list = windowFoodService.queryFtypeByPage(baseFormMap , ftype , orderType ,orderDirection);
			pageView.setRecords(userManageService.list(list));//不调用默认分页,调用自已的mapper中findUserPage
	        return pageView;
	}
	
	
	@ResponseBody
	@RequestMapping("order")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	public String order(HttpServletRequest request) throws Exception {
		String[] ids = getParaValues("ids");
		String data[] = ids[0].split("#");
		System.out.println(ids[0]);
		User user =(User) request.getSession().getAttribute("user");
		System.out.println(user);
		
		return userManageService.addOrder(data,user.getId());
	}
	
	
	@RequestMapping("selectOB")
	public String selectOB(Model model) throws Exception {
//		model.addAttribute("oblist", windowService.list());
//		model.addAttribute("buttons", buttonService.getButtons("#user"));
		return Common.BACKGROUND_PATH + "/system/usermanage/selectOB";
	}
	
	
	@ResponseBody
	@RequestMapping("selectOBByPage")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	public PageView selectOBByPage(String pageNow,String pageSize ,HttpServletRequest request) throws Exception {
		BaseFormMap baseFormMap = getFormMap(BaseFormMap.class);
		baseFormMap=toFormMap(baseFormMap, pageNow, pageSize);
		
		String isbuy = (String)baseFormMap.get("isbuy");
		if(isbuy == null) isbuy = "0";
		User user =(User) request.getSession().getAttribute("user");
		System.out.println(user);
		List<Order> list = orderService.listByPage(user.getId(), isbuy , baseFormMap);
		System.out.println(list);
		pageView.setRecords(list);//不调用默认分页,调用自已的mapper中findUserPage
        return pageView;
	}
	
	@ResponseBody
	@RequestMapping("orderToBuy")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	public String orderToBuy() throws Exception {
		String[] ids = getParaValues("ids");
		String[] iids = ids[0].split(",");
		return userManageService.orderBuy(iids);
	}
	
	@RequestMapping("comments")
	public String comments(Model model) throws Exception {
		model.addAttribute("windows", windowService.list());
//		model.addAttribute("buttons", buttonService.getButtons("#user"));
		return Common.BACKGROUND_PATH + "/system/usermanage/comments";
	}
	
	
	@ResponseBody
	@RequestMapping("selectCommentsByPage")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	public PageView selectCommentsByPage(String pageNow,String pageSize ,HttpServletRequest request) throws Exception {
		BaseFormMap baseFormMap = getFormMap(BaseFormMap.class);
		baseFormMap=toFormMap(baseFormMap, pageNow, pageSize);
		
		String isbuy = (String)baseFormMap.get("isbuy");
		if(isbuy == null) isbuy = "0";
		User user =(User) request.getSession().getAttribute("user");
		System.out.println(user);
		List<Order> list = orderService.listByPage(user.getId(), isbuy , baseFormMap);
		System.out.println(list);
		pageView.setRecords(list);//不调用默认分页,调用自已的mapper中findUserPage
        return pageView;
	}
	
	@ResponseBody
	@RequestMapping("addComment")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	public PageView addComment(String pageNow,String pageSize ,HttpServletRequest request) throws Exception {
		BaseFormMap baseFormMap = getFormMap(BaseFormMap.class);
		baseFormMap=toFormMap(baseFormMap, pageNow, pageSize);
		String wid = (String)baseFormMap.get("wid");
		String content = (String)baseFormMap.get("content");
		User user =(User) request.getSession().getAttribute("user");
		List<Window> lw = windowService.list();
		pageView.setRecords(lw);//不调用默认分页,调用自已的mapper中findUserPage
		if(wid == null) return pageView;
		userManageService.saveComment(wid, content, user);
        return pageView;
	}
	
	
}